package com.sanmu.shop.util;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: ExceptionHandle
 * @Description:
 * @author lilj
 * @date 2018年4月16日 上午12:04:41
 */
@ControllerAdvice
public class ExceptionHandle {

	@SuppressWarnings("rawtypes")
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public Result handle(Exception e) {
		if (e instanceof UserException) {
			UserException base = (UserException) e;
			e.printStackTrace();
			return ResultUtil.error(base.getErrorCode(), base.getErrorMessage());
		} else if (e instanceof NullPointerException) {
			e.printStackTrace();
			return ResultUtil.error("-2", "空指针异常！" + e.toString());
		} else {
			e.printStackTrace();
			return ResultUtil.error("9999", e.getMessage());
		}
	}
}
